"""
插件管理Schema
"""
from apps.extensions import ma
from marshmallow import fields, validates, ValidationError
from datetime import datetime


class SysPluginSchema(ma.Schema):
    """插件管理Schema"""
    id = fields.Int(dump_only=True)
    name = fields.Str(required=True, description='插件名称')
    version = fields.Str(required=True, description='插件版本')
    description = fields.Str(allow_none=True, description='插件描述')
    author = fields.Str(allow_none=True, description='插件作者')
    blueprint_name = fields.Str(allow_none=True, description='蓝图名称')
    url_prefix = fields.Str(allow_none=True, description='URL前缀')
    plugin_path = fields.Str(allow_none=True, description='插件路径')
    config_file = fields.Str(allow_none=True, description='配置文件路径')
    
    # 状态相关
    status = fields.Int(description='状态：0-禁用，1-启用')
    is_installed = fields.Bool(description='是否已安装')
    is_enabled = fields.Bool(description='是否已启用')
    
    # 文件信息
    file_size = fields.Int(allow_none=True, description='文件大小(字节)')
    file_hash = fields.Str(allow_none=True, description='文件哈希值')
    
    # 时间信息
    created_at = fields.DateTime(dump_only=True, description='创建时间')
    updated_at = fields.DateTime(dump_only=True, description='更新时间')
    installed_at = fields.DateTime(allow_none=True, description='安装时间')
    enabled_at = fields.DateTime(allow_none=True, description='启用时间')
    
    # 扩展信息
    extra_info = fields.Dict(allow_none=True, description='扩展信息')
    
    # 排序
    sort_order = fields.Int(description='排序')
    
    @validates('name')
    def validate_name(self, value):
        """验证插件名称"""
        if not value or not value.strip():
            raise ValidationError('插件名称不能为空')
        if len(value.strip()) > 100:
            raise ValidationError('插件名称长度不能超过100个字符')
    
    @validates('version')
    def validate_version(self, value):
        """验证版本号"""
        if not value or not value.strip():
            raise ValidationError('版本号不能为空')
        # 简单的版本号格式验证
        import re
        if not re.match(r'^\d+\.\d+(\.\d+)?$', value):
            raise ValidationError('版本号格式不正确，应为 x.y 或 x.y.z 格式')


class SysPluginCreateSchema(ma.Schema):
    """创建插件Schema"""
    name = fields.Str(required=True, description='插件名称')
    version = fields.Str(required=True, description='插件版本')
    description = fields.Str(allow_none=True, description='插件描述')
    author = fields.Str(allow_none=True, description='插件作者')
    installed_at = fields.DateTime(allow_none=True, description='安装时间')


class SysPluginUpdateSchema(ma.Schema):
    """更新插件Schema"""
    name = fields.Str(description='插件名称')
    version = fields.Str(description='插件版本')
    description = fields.Str(allow_none=True, description='插件描述')
    author = fields.Str(allow_none=True, description='插件作者')
    status = fields.Int(description='状态：0-禁用，1-启用')
    is_installed = fields.Bool(description='是否已安装')
    is_enabled = fields.Bool(description='是否已启用')


class SysPluginQuerySchema(ma.Schema):
    """插件查询Schema"""
    page = fields.Int(missing=1, description='页码')
    limit = fields.Int(missing=20, description='每页数量')
    name = fields.Str(allow_none=True, description='插件名称')
    author = fields.Str(allow_none=True, description='插件作者')
    status = fields.Int(allow_none=True, description='状态')
    is_installed = fields.Bool(allow_none=True, description='是否已安装')
    is_enabled = fields.Bool(allow_none=True, description='是否已启用')
    sort_field = fields.Str(missing='id', description='排序字段')
    sort_order = fields.Str(missing='desc', description='排序方向')


class SysPluginLogSchema(ma.Schema):
    """插件日志Schema"""
    id = fields.Int(dump_only=True)
    plugin_id = fields.Int(required=True, description='插件ID')
    operation = fields.Str(required=True, description='操作类型')
    operation_desc = fields.Str(allow_none=True, description='操作描述')
    operator = fields.Str(allow_none=True, description='操作人')
    ip_address = fields.Str(allow_none=True, description='IP地址')
    user_agent = fields.Str(allow_none=True, description='用户代理')
    success = fields.Bool(description='是否成功')
    error_message = fields.Str(allow_none=True, description='错误信息')
    created_at = fields.DateTime(dump_only=True, description='创建时间')
    
    # 关联信息
    plugin_name = fields.Str(dump_only=True, description='插件名称')


class SysPluginLogQuerySchema(ma.Schema):
    """插件日志查询Schema"""
    page = fields.Int(missing=1, description='页码')
    limit = fields.Int(missing=20, description='每页数量')
    plugin_id = fields.Int(allow_none=True, description='插件ID')
    operation = fields.Str(allow_none=True, description='操作类型')
    operator = fields.Str(allow_none=True, description='操作人')
    success = fields.Bool(allow_none=True, description='是否成功')
    start_date = fields.Date(allow_none=True, description='开始日期')
    end_date = fields.Date(allow_none=True, description='结束日期')
    sort_field = fields.Str(missing='id', description='排序字段')
    sort_order = fields.Str(missing='desc', description='排序方向') 